package com.qjc.excel.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.qjc.excel.entity.CourseEntity;
import com.qjc.excel.entity.StudentEntity;
import com.qjc.excel.entity.TeacherEntity;
import com.qjc.excel.utils.DateUtils;
import com.qjc.excel.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @Description:
 * @Author: qjc
 * @Date: 2019/11/27
 */
@Api(value = "EasyPOI接口", tags = {"EasyPOI接口"})
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.PUT})
@Slf4j
public class FileController {

    @ApiOperation(value = "对象定义--导出数据到Excel", notes = "对象定义--导出数据到Excel")
    @RequestMapping(value = "/exportExcel/object", method = RequestMethod.GET)
    public void exportData(HttpServletResponse response) {
        //从数据库中查询数据，直接用浏览器访问http://localhost:8777/exportExcel/object即可
        List<StudentEntity> studentEntityList = createStudents();

        //导出操作
        ExcelUtil.exportExcel(studentEntityList, "四大学生", "EasyPOISheet", StudentEntity.class, "学生.xls", response);
    }

    @ApiOperation(value = "导入Excel", notes = "导入Excel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public void importExcel(@RequestParam("textFile") MultipartFile file) throws Exception {
        //解析excel，直接通过swagger上传Excel就行
        List<StudentEntity> studentEntityList = ExcelUtil.importExcel(file, 1, 1, StudentEntity.class);
        for (StudentEntity studentEntity : studentEntityList) {
            System.err.println(studentEntity.toString());
        }
    }

    @ApiOperation(value = "集合定义--导出数据到Excel", notes = "集合定义--导出数据到Excel")
    @RequestMapping(value = "/importExcel/collection", method = RequestMethod.GET)
    public void importCollectionExcel(HttpServletResponse response) {
        //浏览器直接访问http://localhost:8777/importExcel/collection
        List<CourseEntity> courseEntityList = new ArrayList<>();

        CourseEntity courseEntity1 = new CourseEntity();
        courseEntity1.setName("生物");
        TeacherEntity teacherEntity1 = new TeacherEntity();
        teacherEntity1.setName("刘备");
        courseEntity1.setMathTeacher(teacherEntity1);
        courseEntity1.setStudents(createStudents());

        courseEntityList.add(courseEntity1);


        CourseEntity courseEntity2 = new CourseEntity();
        courseEntity2.setName("化学");
        TeacherEntity teacherEntity2 = new TeacherEntity();
        teacherEntity2.setName("曹操");
        courseEntity2.setMathTeacher(teacherEntity2);
        courseEntity2.setStudents(createStudents());

        courseEntityList.add(courseEntity2);

        //导出操作
        ExcelUtil.exportExcel(courseEntityList, "课程-老师-学生", "course", CourseEntity.class, "课程.xls", response);
    }


    /**
     * 创建学生集合
     *
     * @return
     */
    private List<StudentEntity> createStudents() {
        List<StudentEntity> studentEntityList = new ArrayList<>();
        StudentEntity studentEntity1 = new StudentEntity();
        studentEntity1.setName("刘德华");
        studentEntity1.setSex(1);
        studentEntity1.setBirthday(DateUtils.formatStringToDate("2010-01-01", "yyyy-MM-dd"));
        studentEntity1.setRegistrationDate(DateUtils.formatStringToDate("2015-01-01", "yyyy-MM-dd"));
        StudentEntity studentEntity2 = new StudentEntity();
        studentEntity2.setName("黎明");
        studentEntity2.setSex(1);
        studentEntity2.setBirthday(DateUtils.formatStringToDate("2009-01-01", "yyyy-MM-dd"));
        studentEntity2.setRegistrationDate(DateUtils.formatStringToDate("2014-01-01", "yyyy-MM-dd"));
        StudentEntity studentEntity3 = new StudentEntity();
        studentEntity3.setName("郭富城");
        studentEntity3.setSex(2);
        studentEntity3.setBirthday(DateUtils.formatStringToDate("2008-01-01", "yyyy-MM-dd"));
        studentEntity3.setRegistrationDate(DateUtils.formatStringToDate("2013-01-01", "yyyy-MM-dd"));
        StudentEntity studentEntity4 = new StudentEntity();
        studentEntity4.setName("张学友");
        studentEntity4.setSex(2);
        studentEntity4.setBirthday(DateUtils.formatStringToDate("2011-01-01", "yyyy-MM-dd"));
        studentEntity4.setRegistrationDate(DateUtils.formatStringToDate("2016-01-01", "yyyy-MM-dd"));

        studentEntityList.add(studentEntity1);
        studentEntityList.add(studentEntity2);
        studentEntityList.add(studentEntity3);
        studentEntityList.add(studentEntity4);
        return studentEntityList;
    }

}
